home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / Text.h < prev    next >
C/C++ Source or Header  |  1992-08-26  |  8KB  |  272 lines

  1. #ifndef Text_First
  2. #ifdef __GNUG__
  3. //pragma once
  4. #pragma interface
  5. #endif
  6. #define Text_First
  7.  
  8. #include "Object.h"
  9. #include "String.h"
  10. #include "Font.h"
  11. #include "Port.h"
  12. #include "Storage.h"
  13. #include "TextStyles.h"
  14. #include "TextFormatter.h"
  15. #include "Mark.h"
  16. class RegularExp;
  17.  
  18. //---- VisualMark ------------------------------------------------------
  19. //     VisualMarks are objects with a visual representation. They
  20. //     can be inserted in a text and behave like characters.
  21.  
  22. class VisualMark: public Mark {
  23. public:
  24.     MetaDef(VisualMark);
  25.     
  26.     VisualMark();
  27.     virtual void CalcExtent();
  28.     virtual Point GetExtent();
  29.     virtual int Base();
  30.     
  31.     virtual void Draw(Point at, Rectangle clip, Rectangle line, bool border);
  32.     virtual void SendDown(int, int, void *);
  33.     virtual void SetContainer(class VObject *);
  34.     
  35.     virtual bool WantsInput(Point lp);
  36.     virtual class Command *Input(Point lp, Token &t, class Clipper *cl);
  37.     
  38.     virtual VObject *GetVObject(); // compatibility
  39. };
  40.  
  41. //---- abstract class Text ----------------------------------------------
  42.  
  43. const int cEOT  = -1,
  44.       cTabw = 40;
  45.  
  46. const bool cSearchForward= TRUE,
  47.        cSearchBackward= !cSearchForward,
  48.        cPaginationChange= TRUE,
  49.        cNoPaginationChange= !cPaginationChange;
  50.        
  51.        
  52. //---- change protocol -------------------------------------------------
  53.  
  54. // text is an abstract class defining methods to manage a buffer for holding 
  55. // text. Together with the text there is a list of robust pointers (called marks)
  56. // into the text. Dependents of the Text classes are notified by 
  57. // the observer mechanism
  58. // and receive a change record as described below as argument
  59.  
  60. enum eTextChanges { 
  61.     eTextChangedRange, 
  62.     eTextDeleted,
  63.     eTextReplaced       // from,to=> replaced range, size=>size of inserted text
  64. };
  65.        
  66. struct TextChanges {
  67.     int from, to, size;
  68.     bool pagination;
  69.     TextChanges *operator()(int f, int t, int s= 0, bool p= cNoPaginationChange)
  70.     { from= f; to= t; size= s; pagination= p; return this; }
  71. };
  72.     
  73. //---- Text -----------------------------------------------------------------
  74.  
  75. class Text: public Object { 
  76. public:    
  77.     MetaDef(Text);
  78.     void InitNew();
  79.  
  80.     ~Text();
  81.  
  82.     //---- editing
  83.     virtual void Insert(byte c, int from, int to);
  84.     virtual void Cut(int from, int to);   
  85.     virtual void Paste(Text *t, int from, int to); 
  86.     virtual void Copy(Text* save, int from, int to);
  87.     void Append(byte c); 
  88.     void Empty();            
  89.  
  90.     // primitive for editing operations
  91.     virtual void ReplaceRange(int from, int to, Text *src, int sfrom, int sto);
  92.     virtual Text *MakeScratchText(byte *buf, int size); 
  93.  
  94.     //---- converting
  95.     virtual void InsertStr(int from, int to, byte *str, int len= -1); 
  96.     virtual void CopyInStr(byte *str, int strSize, int from, int to); 
  97.     void ReplaceWithStr(byte *str, int len= -1); 
  98.     char *AsString();                       // allocates new buffer !!!
  99.  
  100.     //---- accessing
  101.     void SetFString(char *fmt, ...);
  102.     Text *Save(int from, int to);
  103.     // allocate new text object and copy the given range
  104.     void SetDefTab(int tw);               // set default tab width
  105.     int GetDefTab();
  106.     
  107.     virtual byte& operator[](int i);  
  108.     virtual int Size();              
  109.     inline int End();                    
  110.     bool IsEmpty();
  111.     
  112.     virtual int Search(
  113.         RegularExp *rex, int *nMatched, int start = 0, 
  114.         int range= cMaxInt, bool dir= cSearchForward
  115.     ); // search rex and return position of match (-1 == no match)
  116.  
  117.  
  118.     //---- style access
  119.     virtual void ResetCurrentCharStyle();
  120.     virtual void SetCharStyle(TxtCharProp mode, int from, int to, const CharStyleSpec &newStyle);
  121.     virtual void SetParaStyle(TxtParaProp mode, int from, int to, const ParaDesc &props);
  122.     virtual CharStyle *GetCharStyle(int at);
  123.     virtual CharStyle *GetCurrentCharStyle();
  124.     virtual ParaStyle *GetParaStyle(int at);
  125.  
  126.     void SetFont(Font *fd, int from= 0, int to= 0); 
  127.     Font *GetFont(int at= 0);
  128.     
  129.     //---- iterator
  130.     virtual class TextIter *MakeIterator(int from=0, int to= cMaxInt); //abstract
  131.  
  132.     //---- text structure access
  133.     bool IsParaStart(int at);
  134.     bool IsParaEnd(int at);
  135.     void GetWordRange(int at, int *start, int *end);
  136.     void GetParaRange(int at, int *start, int *end);
  137.     static bool IsBreak(byte ch);
  138.     static bool IsPara(byte ch);
  139.     int FindLastBreak(int from);
  140.     int FindNextBreak(int from);
  141.     
  142.     //---- TextPainter
  143.     virtual byte *GetLineAccess(byte **buf, int from, int to);
  144.     virtual int GetNextFontChange(int at, CharStyle *&sp);
  145.     
  146.     //---- VisualMarks embedding
  147.     virtual VisualMark *GetVisualMarkAt(int at);
  148.     virtual byte GetMarkChar();
  149.     virtual bool IsVisualMark(int at);
  150.  
  151.     //---- Marks
  152.     void AddMark(class Mark *);
  153.     Mark *RemoveMark(class Mark *);
  154.     class Iterator *GetMarkIter(); 
  155.     class MarkList *GetMarkList();   
  156.  
  157.     //---- input/output
  158.     virtual OStream& PrintOnAsPureText(OStream &s);
  159.     virtual IStream& ReadFromAsPureText(IStream &s, long sizeHint= -1);
  160.     OStream& PrintOn(OStream&);
  161.     IStream& ReadFrom(IStream&);
  162.  
  163.     //---- generic object methods
  164.     u_long Hash ();
  165.     bool IsEqual(Object *t);
  166.  
  167.     //---- inspecting 
  168.     void InspectorId(char *b, int s);
  169.     
  170.     //---- static
  171.     static bool CheckRange (int max, int from, int to);
  172.  
  173. protected:
  174.     CharStyle *charStyle;
  175.     ParaStyle *paraStyle;
  176.  
  177.     Text(); 
  178.     void Init();
  179.     bool IsTerminated();
  180.     
  181.     virtual void SetFStringVL(char *fmt, va_list ap); 
  182.        
  183.     virtual int GrowSize(int minSize);
  184.    
  185.     // overridden for optimized observing
  186.     class Collection *MakeObserverColl();
  187.     class Collection *GetObservers();
  188.     void DestroyObserverColl();
  189.     void SetObserverColl(Collection *);
  190.  
  191. private:
  192.     class MarkList *marks;
  193.     class Collection *observers;  
  194.     int tabWidth;                                                         
  195. };
  196.  
  197. //---- character formatting properties ----------------------------
  198.  
  199. extern byte TextCharMap[];
  200.  
  201. const byte cTextPara    =       01,     // paragraph terminator
  202.        cTextBreak   =       02;     // break
  203.        
  204. inline bool Text::IsBreak(byte ch)
  205. {
  206.     return Iscntrl(ch) && (TextCharMap[ch]&cTextBreak); 
  207. }
  208.  
  209. inline bool Text::IsPara(byte ch)
  210. {
  211.     return Iscntrl(ch) && (TextCharMap[ch]&cTextPara); 
  212. }
  213.  
  214. inline int Text::End()
  215. {
  216.     return Size()-1;
  217. }
  218.  
  219. inline bool Text::IsTerminated()
  220. {
  221.     return (Size() > 0 && (*this)[Size()-1] == '\0');
  222. }
  223.            
  224. inline bool Text::CheckRange (int max, int from, int to)
  225. {
  226.     return (to > max || from < 0 || from > to) ? FALSE: TRUE;
  227. }
  228.  
  229. //---- abstract class TextIter ----------------------------------------------
  230.  
  231. class TextIter {
  232. protected:
  233.     int ce, upto, unget;
  234.     Text *ct;
  235. public:
  236.     TextIter(Text *s, int from= 0, int to= cMaxInt);        
  237.     virtual ~TextIter();                                // abstract
  238.     virtual int operator()(int *width= 0, LineDesc* l= 0); // abstract                                          
  239.     virtual void Reset(Text *s, int from= 0, int to= cMaxInt);        
  240.     virtual int Token(int *width, LineDesc* l= 0);      // return next token and width, abstract
  241.     virtual int GetPos();
  242.     virtual Font *FontAt(int);
  243.     virtual int GetLastPos();                           // get last position
  244.     virtual void SetPos(int newPos);
  245.     virtual int Unget();                                // unget last token
  246.  
  247.     //---- memory allocation
  248.     void *operator new (size_t sz)
  249.     { return Storage::ChunkAlloc(sz); }
  250.     void operator delete(void *vp)
  251.     { Storage::ChunkFree(vp); }
  252. };
  253.  
  254. class AutoTextIter { 
  255.     TextIter *ti;
  256. public:
  257.     AutoTextIter(Text *t, int from= 0, int to= cMaxInt);
  258.     AutoTextIter(TextIter *tip)
  259.     { ti= tip; }
  260.     ~AutoTextIter()
  261.     { if (ti) delete ti; }
  262.     int operator()(int *width= 0, LineDesc* l= 0) 
  263.     { return (*ti)(width, l); }
  264.     TextIter *operator->()
  265.     { return ti; }
  266.     operator TextIter*()
  267.     { return ti; }
  268. };
  269.  
  270. #endif
  271.  
  272.